package com.copera.cn.kaka;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.copera.cn.kaka.util.ActivityManager;

public class BaseActivity extends Activity {

	private FrameLayout mContainer;
	private ApplicationCustomer mApp;
	public boolean end;
	public int pageSize = 10;
	public int currentPage = 1;
	public int visibleLastIndex = 0;
	public int visibleItemCount;
	public TextView pgtext;
	public ProgressBar pg;
	public View moreView;
	public int totalcount;
	public int currentcount;
	public Handler handler;
	private ProgressDialog pDialog;

	public ProgressDialog getpDialog(Context context) {
		pDialog = new ProgressDialog(context);
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.setMessage("数据加载中...");
		pDialog.setIndeterminate(false);
		return pDialog;
	}

	public void setpDialog(ProgressDialog pDialog) {
		this.pDialog = pDialog;
	}

	public int getVisibleLastIndex() {
		return visibleLastIndex;
	}

	public void setVisibleLastIndex(int visibleLastIndex) {
		this.visibleLastIndex = visibleLastIndex;
	}

	public int getVisibleItemCount() {
		return visibleItemCount;
	}

	public void setVisibleItemCount(int visibleItemCount) {
		this.visibleItemCount = visibleItemCount;
	}

	public boolean isEnd() {
		return end;
	}

	public void setEnd(boolean end) {
		this.end = end;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void loadMore() {

	}
    private SharedPreferences.Editor editor;
    private SharedPreferences settings;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().push(this);
		setContentView(R.layout.common_tab);

		mApp = (ApplicationCustomer) getApplication();
		mContainer = (FrameLayout) findViewById(R.id.container_common);
		moreView = getLayoutInflater().inflate(R.layout.moredata, null);
		pg = (ProgressBar) moreView.findViewById(R.id.pg);
		pgtext = (TextView) moreView.findViewById(R.id.pgtext);
		handler = new Handler();
		settings = getSharedPreferences("setting", 0);
		editor = settings.edit();

	}
    public void save(String name,String value){
    	editor.putString(name,value);
    	editor.commit();
    }
    public String getValue(String name){
    	return settings.getString(name, "");
    }
	public List<NameValuePair> getParams(EditText... editTexts) {
		List<NameValuePair> params = null;
		if (editTexts != null && editTexts.length > 0) {
			params = new ArrayList<NameValuePair>();
			for (EditText editText : editTexts) {
				String name = (String) editText.getTag();
				String value = editText.getText().toString();
				params.add(new BasicNameValuePair(name, value));
			}
		}

		return params;
	}

	protected void refreshView(View v) {
		mContainer.removeAllViews();
		mContainer.addView(v);
	}

	protected ApplicationCustomer getAppCustomer() {
		return mApp;
	}

	protected void alert(String title, String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		if (title == null) {
			builder.setTitle("");
		} else {
			builder.setTitle(title);
		}
		builder.setMessage(msg);
		builder.setNegativeButton("", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}

	public void appExit() {
		ActivityManager.getInstance().exit();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();

		}

		return false;

	}

	/**
	 * 转换编码
	 */
	public static String changeToUnicode(String str) {

		StringBuffer strBuff = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			String temp = Integer.toHexString(str.charAt(i));
			if (temp.length() != 4) {
				temp = "00" + temp;
			}
			if (temp.equals("00d")) {
				temp = "0" + temp;
			}
			if (temp.equals("00a")) {
				temp = "0" + temp;
			}
			strBuff.append(temp.substring(0, temp.length() - 2));
			strBuff.append(temp.substring(temp.length() - 2, temp.length()));
		}
		String returnData = strBuff.toString();
		return returnData;
	}
}
